package com.example.personalcenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * 首页Fragment - 显示根据兴趣推荐的内容
 */
public class HomeFragment extends Fragment {
    
    private TextView tvRecommendation;
    private SharedPreferences sharedPreferences;
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        
        tvRecommendation = view.findViewById(R.id.tv_recommendation);
        
        // 获取SharedPreferences
        sharedPreferences = requireActivity().getSharedPreferences("PersonalCenter", Context.MODE_PRIVATE);
        
        // 加载并显示推荐内容
        loadRecommendation();
        
        return view;
    }
    
    @Override
    public void onResume() {
        super.onResume();
        // 每次恢复时刷新推荐内容（兴趣可能已更新）
        loadRecommendation();
    }
    
    /**
     * 加载并显示推荐内容
     */
    private void loadRecommendation() {
        String interest = sharedPreferences.getString("interest", "未选择");
        tvRecommendation.setText("根据您的兴趣" + interest + "，推荐内容如下");
    }
    
    /**
     * 公开方法，供外部调用刷新推荐内容
     */
    public void refreshRecommendation() {
        loadRecommendation();
    }
}
